import cv2

class DrawUtil:

    # 画点 ''' 调试用的: 标记一个点 '''
    @staticmethod
    def draw_point(img, center):
       x, y = center
       # cv2.rectangle(img, (x, y), (x+10, y+10), 255, 1, lineType=cv2.CV_AA)
       radius = 20
       cv2.circle(img, (x, y), radius, (0, 0, 255), thickness=2)
       cv2.line(img, (x-radius, y), (x+radius, y), (0, 0, 255), thickness=1) # x line
       cv2.line(img, (x, y-radius), (x, y+radius), (0, 0, 255), thickness=1) # y line
       return img

    # 用4个点画矩形
    @staticmethod
    def draw_rectangle(img, points):
        # 初始化左上角和右下角的坐标
        top_left = (99999, 99999)
        bottom_right = (0, 0)

        # 遍历所有点，更新左上角和右下角的坐标
        for point in points:
            x, y = point
            if x < top_left[0] and y < top_left[1]:
                top_left = (x, y)
            if x > bottom_right[0] and y > bottom_right[1]:
                bottom_right = (x, y)

        # 画矩形，可以指定矩形的颜色和厚度，这里使用绿色，厚度为3
        cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 2)

        return img

    # 4个点，算出中心点
    @staticmethod
    def cal_center(points):
        # 分别计算x坐标和y坐标的平均值
        x_avg = sum([point[0] for point in points]) / len(points)
        y_avg = sum([point[1] for point in points]) / len(points)

        # 中心点
        center_point = [int(x_avg), int(y_avg)]

        # print("中心点:", center_point)
        return center_point
